package applications.camoverlay;

import java.io.File;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class ImageAdapter implements ListAdapter{

	private String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/";
	private File dir=new File(path);
	private File[] files;
	private Context c;
	private static ImageView views[];
	private static Bitmap bitmaps[];

	public ImageAdapter(Context context){
		files=dir.listFiles();
		
		int picCount=0;
		for(int i=0;i<files.length;i++){
			if(files[i].getName().endsWith(".jpg")||files[i].getName().endsWith(".JPG"))
				picCount++;
		}
		
		File[]picFiles=new File[picCount];
		picCount=0;
		
		for(int i=0;i<files.length;i++){
			if(files[i].getName().endsWith(".jpg")||files[i].getName().endsWith(".JPG"))
				picFiles[picCount++]=files[i];
		}
		
		files=picFiles;
		
		c=context;
		
		views = new ImageView[files.length];
		bitmaps = new Bitmap[files.length];
		
		for(int i=0;i<files.length;i++){
			bitmaps[i]=BitmapFactory.decodeFile(path+files[i].getName());
			views[i]=new ImageView(c);
			views[i].setImageBitmap(Bitmap.createScaledBitmap(bitmaps[i], 100,(bitmaps[i].getHeight()*100)/bitmaps[i].getWidth(), false));
		}
	}
	
	public String getFile(int i){
		return files[i].getName();
	}

	public View getView (int position, View convertView, ViewGroup parent){
		return views[position];
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return bitmaps[position]!=null;
	}

	public int getCount() {
		return files.length;
	}

	public Object getItem(int position) {
		return bitmaps[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return 0;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return files.length==0;
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		
	}
}
